<?php
namespace app\admin\controller;
use think\Controller;
use app\common\model\Conf as Confmodel;

class Conf extends Controller
{
    private $conf;

    public function _initialize(){
        $this->conf = new Confmodel();
    }

    public function edit(){
        if(request()->isPost()){
            $data=input('post.');
            $id=input('id');
            $yan=validate('Conf');
            if($yan->scene('edit')->check($data)){
                if($this->conf->save($data,$id)){
                    $this->success('修改配置成功！','Conf/lst');
                }else{
                    $this->error('修改配置失败！');
                }
            }else{
                $this->error($yan->getError());
            }
        }else{
            $id=input('id');
            $data=$this->conf->find($id);
            $this->assign('data',$data);
            return view();
        }
    }

    public function conflst(){
    	return view();
    }

    public function lst(){
        $data=$this->conf->select();
        $this->assign('data',$data);
    	return view();
    }

    public function add(){
    	if(request()->isPost()){
    		$data=input('post.');
            $yan=validate('Conf');
            if($yan->scene('add')->check($data)){
                if($this->conf->save($data)){
                    $this->success('添加配置成功！','Conf/lst');
                }else{
                    $this->error('添加配置失败，请稍后再试！');
                }
            }else{
                $this->error($yan->getError());
            }
    	}else{
           return view(); 
        }
    }

    public function del(){
        $id=input('id');
        if($this->conf->destroy($id)){
            $this->success('删除配置成功！','Conf/lst');
        }else{
            $this->error('删除配置失败！');
        }
    }

    public function ajaxdel(){
        $data=input('post.');
        if($data){
            $ids=implode(',', $data['data']);
            $r=$this->conf->destroy($ids);
            if($r){
                $res=[
                    'status'=>1,
                    'msg'=>'批量删除配置成功！',
                ];
            }else{
                $res=[
                    'status'=>0,
                    'msg'=>'删除配置失败！',
                ];
            }
        }else{
            $res=[
                'status'=>0,
                'msg'=>'至少选择一个删除！',
            ];
        }
        return $res;

    }
}
